<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>rank_feature 查询 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'query-dsl-rank-feature-query.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-rank-feature-query.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-rank-feature-query.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/query-dsl-rank-feature-query.html" rel="nofollow" target="_blank">../en/query-dsl-rank-feature-query.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="query-dsl.html">查询领域特定语言(Query DSL)</a></span>
»
<span class="breadcrumb-link"><a href="specialized-queries.html">专业的查询</a></span>
»
<span class="breadcrumb-node">rank_feature 查询</span>
</div>
<div class="navheader">
<span class="prev">
<a href="_notes_4.html">« 注意</a>
</span>
<span class="next">
<a href="query-dsl-script-query.html">script 查询 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="query-dsl-rank-feature-query"></a>rank_feature 查询
</h2>
</div></div></div>

<p>
基于 <a class="xref" href="rank-feature.html" title="rank_feature 数据类型"><code class="literal">rank_feature</code></a> 或 <a class="xref" href="rank-features.html" title="rank_features 数据类型" rel="nofollow"><code class="literal">rank_features</code></a> 字段的数值提升文档的<a class="xref" href="query-filter-context.html#relevance-scores" title="Relevance scores" rel="nofollow">相关性评分</a>。
</p>
<p>
<code class="literal">rank_feature</code> 查询通常用在 <a class="xref" href="query-dsl-bool-query.html" title="Boolean query"><code class="literal">bool</code></a> 查询的 <code class="literal">should</code> 子句中，因此它的相关性分数会添加到 <code class="literal">bool</code>查询的其他分数中。
</p>
<p>
与 <a class="xref" href="query-dsl-function-score-query.html" title="Function score query"><code class="literal">function_score</code></a> 查询或其他更改<a class="xref" href="query-filter-context.html#relevance-scores" title="Relevance scores">相关性评分</a>的方法不同，当 <a class="xref" href="search-uri-request.html" title="URI search"><code class="literal">track_total_hits</code></a> 参数<span class="strong strong"><strong>不</strong></span>为<code class="literal">true</code>时，<code class="literal">rank_feature</code> 查询会有效地跳过非竞争命中。

这可以极大地提高查询速度。
</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="rank-feature-query-functions"></a>等级特征(rank feature)函数
</h3>
</div></div></div>
<p>
为了基于等级特征字段计算相关性评分，<code class="literal">rank_feature</code> 查询支持以下数学函数：
</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<a class="xref" href="query-dsl-rank-feature-query.html#rank-feature-query-saturation" title="Saturation">Saturation</a>
</li>
<li class="listitem">
<a class="xref" href="query-dsl-rank-feature-query.html#rank-feature-query-logarithm" title="Logarithm">Logarithm</a>
</li>
<li class="listitem">
<a class="xref" href="query-dsl-rank-feature-query.html#rank-feature-query-sigmoid" title="Sigmoid">Sigmoid</a>
</li>
</ul>
</div>
<p>
如果你不知道从哪里开始，我们建议使用 <code class="literal">saturation</code> 函数。

如果没有指定函数，默认情况下，<code class="literal">rank_feature</code> 查询使用 <code class="literal">saturation</code> 函数。
</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="rank-feature-query-ex-request"></a>请求示例
</h3>
</div></div></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="rank-feature-query-index-setup"></a>创建索引
</h4>
</div></div></div>
<p>
要使用 <code class="literal">rank_feature</code> 查询，索引必须包含<a class="xref" href="rank-feature.html" title="Rank feature datatype" rel="nofollow"><code class="literal">rank_feature</code></a> 或 <a class="xref" href="rank-features.html" title="Rank features datatype" rel="nofollow"><code class="literal">rank_features</code></a>字段映射。

要了解如何为 <code class="literal">rank_feature</code> 查询创建索引，请尝试以下示例。
</p>
<p>使用以下字段映射创建一个名为 <code class="literal">test</code> 的索引：</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<code class="literal">pagerank</code>，一个 <a class="xref" href="rank-feature.html" title="Rank feature datatype"><code class="literal">rank_feature</code></a> 类型的字段，用来衡量一个网站的重要性。
</li>
<li class="listitem">
<code class="literal">url_length</code>，一个 <a class="xref" href="rank-feature.html" title="Rank feature datatype"><code class="literal">rank_feature</code></a> 类型的字段，包含网站url的长度。

在本例中，<code class="literal">positive_score_impact</code> 值设置为 <code class="literal">false</code> 表示长 URL 与相关性负相关。
</li>
<li class="listitem">
<code class="literal">topics</code>，一个 <a class="xref" href="rank-features.html" title="Rank features datatype" rel="nofollow"><code class="literal">rank_features</code></a> 类型的字段，包含一个主题列表和一个衡量每个文档与该主题关联程度的度量
</li>
</ul>
</div>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /test
{
  "mappings": {
    "properties": {
      "pagerank": {
        "type": "rank_feature"
      },
      "url_length": {
        "type": "rank_feature",
        "positive_score_impact": false
      },
      "topics": {
        "type": "rank_features"
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/251.console"></div>
<p>添加并索引几个文档到索引 <code class="literal">test</code> 中：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /test/_doc/1?refresh
{
  "url": "http://en.wikipedia.org/wiki/2016_Summer_Olympics",
  "content": "Rio 2016",
  "pagerank": 50.3,
  "url_length": 42,
  "topics": {
    "sports": 50,
    "brazil": 30
  }
}

PUT /test/_doc/2?refresh
{
  "url": "http://en.wikipedia.org/wiki/2016_Brazilian_Grand_Prix",
  "content": "Formula One motor race held on 13 November 2016",
  "pagerank": 50.3,
  "url_length": 47,
  "topics": {
    "sports": 35,
    "formula one": 65,
    "brazil": 20
  }
}

PUT /test/_doc/3?refresh
{
  "url": "http://en.wikipedia.org/wiki/Deadpool_(film)",
  "content": "Deadpool is a 2016 American superhero film",
  "pagerank": 50.3,
  "url_length": 37,
  "topics": {
    "movies": 60,
    "super hero": 65
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/252.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="rank-feature-query-ex-query"></a>查询示例
</h4>
</div></div></div>
<p>
下面这个查询搜索 <code class="literal">2016</code>，并根据 <code class="literal">pagerank</code>、<code class="literal">url_length</code>和<code class="literal">sports</code>主题(topic.sports) 提升相关性评分。
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "2016"
          }
        }
      ],
      "should": [
        {
          "rank_feature": {
            "field": "pagerank"
          }
        },
        {
          "rank_feature": {
            "field": "url_length",
            "boost": 0.1
          }
        },
        {
          "rank_feature": {
            "field": "topics.sports",
            "boost": 0.4
          }
        }
      ]
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/253.console"></div>
</div>

</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="rank-feature-top-level-params"></a><code class="literal">rank_feature</code>的顶级参数
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">field</code>
</span>
</dt>
<dd>
(必需, string) 用来提升<a class="xref" href="query-filter-context.html#relevance-scores">相关性评分</a>的<a class="xref" href="rank-feature.html"><code class="literal">rank_feature</code></a>或<a class="xref" href="rank-features.html"><code class="literal">rank_features</code></a>类型的字段。
</dd>
<dt>
<span class="term">
<code class="literal">boost</code>
</span>
</dt>
<dd>
<p>(可选, float) 用以降低或提升<a class="xref" href="query-filter-context.html#relevance-scores">相关性评分</a>的浮点数。默认为 <code class="literal">1.0</code>。</p>
<p>

提升值是相对于默认值<code class="literal">1.0</code>的。

提升值在 <code class="literal">0</code> 和 <code class="literal">1.0</code> 之间会降低相关性评分，而当它大于 <code class="literal">1.0</code> 才会增加相关性评分。
</p>
</dd>
<dt>
<span class="term">
<code class="literal">saturation</code>
</span>
</dt>
<dd>
<p>
(可选, <a class="xref" href="query-dsl-rank-feature-query.html#rank-feature-query-saturation" title="Saturation">function object</a>)

用于根据 rank_feature 类型的 <code class="literal">field</code> 的值提升<a class="xref" href="query-filter-context.html#relevance-scores" title="Relevance scores">相关性评分</a>的饱和度函数。

如果未指定函数，<code class="literal">rank_feature</code> 查询默认使用 <code class="literal">saturation</code> 函数。

更多信息请参考 <a class="xref" href="query-dsl-rank-feature-query.html#rank-feature-query-saturation" title="Saturation">饱和度(saturation)</a>。
</p>
<p>
只能指定 <code class="literal">saturation</code>、<code class="literal">log</code> 或 <code class="literal">sigmoid</code> 函数中的一个。
</p>
</dd>
<dt>
<span class="term">
<code class="literal">log</code>
</span>
</dt>
<dd>
<p>
(可选, <a class="xref" href="query-dsl-rank-feature-query.html#rank-feature-query-logarithm" title="Logarithm">function object</a>)

用于根据 rank_feature 类型的 <code class="literal">field</code> 的值提升<a class="xref" href="query-filter-context.html#relevance-scores">相关性评分</a>的对数函数。

更多信息请参考 <a class="xref" href="query-dsl-rank-feature-query.html#rank-feature-query-logarithm" title="Logarithm">对数(logarithm)</a>。
</p>
<p>
只能指定 <code class="literal">saturation</code>、<code class="literal">log</code> 或 <code class="literal">sigmoid</code> 函数中的一个。
</p>
</dd>
<dt>
<span class="term">
<code class="literal">sigmoid</code>
</span>
</dt>
<dd>
<p>
(可选, <a class="xref" href="query-dsl-rank-feature-query.html#rank-feature-query-sigmoid" title="Sigmoid">function object</a>)

用于根据 rank_feature 类型的 <code class="literal">field</code> 的值提升<a class="xref" href="query-filter-context.html#relevance-scores">相关性评分</a>的 sigmoid 函数。


更多信息请参考 <a class="xref" href="query-dsl-rank-feature-query.html#rank-feature-query-sigmoid" title="Sigmoid">sigmoid</a>。
</p>
<p>
只能指定 <code class="literal">saturation</code>、<code class="literal">log</code> 或 <code class="literal">sigmoid</code> 函数中的一个。
</p>
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="rank-feature-query-notes"></a>注意
</h3>
</div></div></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="rank-feature-query-saturation"></a>saturation
</h4>
</div></div></div>
<p>
<code class="literal">saturation</code> 函数给出等于 <code class="literal">S / (S + pivot)</code> 的分数，其中 <code class="literal">S</code> 是 rank_feature 类型字段的值，<code class="literal">pivot</code> 是可配置的 pivot 值，因此如果 <code class="literal">S</code> 小于 pivot，结果将小于 <code class="literal">0.5</code>，否则大于 <code class="literal">0.5</code>。

评分总是位于 0 和 1 之间, 即 <code class="literal">(0,1)</code>。
</p>
<p>
如果等级特征具有负得分影响<span class="remark">(即 positive_score_impact 为 false)</span>，则该函数将被计算为 <code class="literal">pivot / (S + pivot)</code>，其值将随着 <code class="literal">S</code>的增加而减少。
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /test/_search
{
  "query": {
    "rank_feature": {
      "field": "pagerank",
      "saturation": {
        "pivot": 8
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/254.console"></div>
<p>
如果未提供 <code class="literal">pivot</code> 值，Elasticsearch 将计算默认值，该值等于索引中所有等级特征值的近似几何平均值。

如果你还没有机会训练一个好的 pivot 值，我们建议使用这个默认值。
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /test/_search
{
  "query": {
    "rank_feature": {
      "field": "pagerank",
      "saturation": {}
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/255.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="rank-feature-query-logarithm"></a>logarithm
</h4>
</div></div></div>
<p>
<code class="literal">log</code> 函数给出的分数等于 <code class="literal">log(scaling_factor + S)</code>，其中 <code class="literal">S</code> 是 rank_feature 类型字段的值，<code class="literal">scaling_factor</code> 是可配置的缩放因子。

其计算的值无界。
</p>
<p>
该函数仅支持对分数有正面影响的等级特征<span class="remark">(即 positive_score_impact 为 true)</span>。
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /test/_search
{
  "query": {
    "rank_feature": {
      "field": "pagerank",
      "log": {
        "scaling_factor": 4
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/256.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="rank-feature-query-sigmoid"></a>sigmoid
</h4>
</div></div></div>
<p>
<code class="literal">sigmoid</code> 函数是 <code class="literal">saturation</code> 的扩展，它增加了一个可配置的 exponent。

按 <code class="literal">S^exp^ / (S^exp^ + pivot^exp^)</code> 计算分数。

与 <code class="literal">saturation</code> 函数一样，<code class="literal">pivot</code> 是 <code class="literal">S</code> 的值，该值给出 <code class="literal">0.5</code>的分数，分数在 <code class="literal">(0,1)</code> 范围内。
</p>
<p>
<code class="literal">exponent</code> 必须是正数，通常在 <code class="literal">[0.5, 1]</code> 范围内。

一个好的值应该通过训练来计算。

如果你没有机会这样做，我们建议使用 <code class="literal">saturation</code> 函数代替之。
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /test/_search
{
  "query": {
    "rank_feature": {
      "field": "pagerank",
      "sigmoid": {
        "pivot": 7,
        "exponent": 0.6
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/257.console"></div>
</div>

</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="_notes_4.html">« 注意</a>
</span>
<span class="next">
<a href="query-dsl-script-query.html">script 查询 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>